<?php


namespace chixian\Libraries\Exception\Handler;


use Psr\Http\Message\ResponseInterface;
use Throwable;
use chixian\Libraries\HttpServer\ResponseInterface as ResponseInterfaceLib;

/**
 * Class ValidationExceptionHandler
 * @package chixian\Libraries\Exception\Handler
 */
class ValidationExceptionHandler extends \Hyperf\Validation\ValidationExceptionHandler
{
    /** @var ResponseInterfaceLib */
    protected $response;

    /**
     * ValidationExceptionHandler constructor.
     * @param ResponseInterfaceLib $response
     */
    public function __construct(ResponseInterfaceLib $response)
    {
        $this->response = $response;
    }

    /**
     * @param Throwable $throwable
     * @param ResponseInterface $response
     * @return ResponseInterface
     */
    public function handle(Throwable $throwable, ResponseInterface $response)
    {
        $this->stopPropagation();
        return $this->response->rJson([], 10500, $throwable->validator->errors()->first());
    }
}